package softtech.apps.callblocker;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Random;

import softtech.apps.callblocker.adapter.AdapterBlackList;
import softtech.apps.callblocker.model.BlackListItem;
import softtech.apps.callblocker.model.CallBlockedItem;
import softtech.apps.callblocker.model.ContentSmsBlock;
import softtech.apps.callblocker.model.DatabaseHanler;
import softtech.apps.callblocker.model.DbConstant;
import softtech.apps.callblocker.model.HeaderPhone;
import softtech.apps.callblocker.model.Setting;
import softtech.apps.callblocker.model.SmsBlockedItem;
import softtech.apps.callblocker.quickAction.ActionItem;
import softtech.apps.callblocker.quickAction.QuickAction;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.SearchView;
import android.widget.SearchView.OnQueryTextListener;
import android.widget.Toast;

public class ActivityTest extends Activity {

	private LinearLayout layoutEnable;
	private ListView lvBlackListItem;

	private List<BlackListItem> listSearch = new ArrayList<BlackListItem>();
	private List<BlackListItem> BlackList = new ArrayList<BlackListItem>();
	private AdapterBlackList adaterBlackList;

	private Button btnPassOk;
	private Button btnFakePass;
	private EditText edtPass;
	private EditText edtFake;

	private RadioGroup group1;
	private RadioGroup group2;
	private RadioButton r11;
	private RadioButton r12;
	private RadioButton r13;
	private RadioButton r14;
	private RadioButton r15;
	private RadioButton r16;
	private RadioButton r21;
	private RadioButton r22;
	private RadioButton r23;

	private ImageButton ibtnAdd;
	private EditText edtAdd;

	private String[] columnsBlacklist = { BlackListItem.NAME,
			BlackListItem.PHONE_NUMBER };

	public static final String TAG = "TAG TEST";

	DatabaseHanler db = new DatabaseHanler(this);
	private String resultSearch = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.viewflipper_blacklist);
		deleteDatabase(DatabaseHanler.DATABASE_NAME);
		deleteRowInData();
		InsertData();
		
	}
	
	public void deleteRowInData(){
		db.deleteCallBlockItem(CallBlockedItem.ID + " =? ", new String[]{"70"});
	}
	public void InsertData() {
		
		Log.i(TAG, "-------- insert setting --------");
		long idSet, idBlack;
		idSet = db.insertSetting(new Setting(1, "123", "123", 17, 1, 1));
		Setting s = new Setting();
		s = db.getSetting(null);
		Log.i(TAG, "Setting: " + s.toString());

		BlackListItem b = new BlackListItem();
		List<BlackListItem> lBlack = new ArrayList<BlackListItem>();
		List<CallBlockedItem> lCall = new ArrayList<CallBlockedItem>();
		List<SmsBlockedItem> lSms = new ArrayList<SmsBlockedItem>();
		List<ContentSmsBlock> listContent = new ArrayList<ContentSmsBlock>();
		List<HeaderPhone> listPhones = new ArrayList<HeaderPhone>();

		Calendar now = Calendar.getInstance();
		String strDateFormat = "dd/MM/yyyy hh:mm:ss a";
		SimpleDateFormat sdf =null;
		sdf= new SimpleDateFormat(strDateFormat);
		Random r = new Random();
		long x, y;
		for (int i = 0; i < 50; i++) {
			db.insertBlackListItem(new BlackListItem("06435599", "vanmui", 1));
			db.insertSmsBlockItem(new SmsBlockedItem(1, String.valueOf(r
					.nextInt(9865746)), sdf.format(now.getTime()), 0, "content"));
			db.insertCallBlockItem(new CallBlockedItem(String.valueOf(r
					.nextInt(9865746)), sdf.format(now.getTime()), 0));
			db.insertContentSmsBlock(new ContentSmsBlock(1, "quang cao"));
			db.insertHeaderPhoneCall(new HeaderPhone(1, "0128"));
			db.insertHeaderPhoneSms(new HeaderPhone(1, "0120"));
		}
		lBlack = db.getBlackListItem(null, null, null, "name", null);
		for (BlackListItem blackListITem : lBlack) {
			Log.i(TAG, "Blaclist: " + blackListITem.toString());
		}

		lCall = db.getCallBlockItem(null, null, null, null, null);
		for (CallBlockedItem c : lCall) {
			Log.i(TAG, c.toString());
		}

		lSms = db.getSmsBlockItem(null, null, null, null, null);
		for (SmsBlockedItem sms : lSms) {
			Log.i(TAG, sms.toString());
		}

		listContent = db.getContentSmsBlocks(null, null, null, null, null);
		listPhones = db.getHeaderPhones(DbConstant.TABLE_HEADER_PHONE_CALL,
				null, null, null, null, null);
		for (ContentSmsBlock content : listContent) {
			Log.i(TAG, content.toString());
		}

		listPhones = db.getHeaderPhones(DbConstant.TABLE_HEADER_PHONE_CALL,
				null, null, null, null, null);
		for (HeaderPhone headerPhone : listPhones) {
			Log.i(TAG, headerPhone.toString());
		}

		listPhones.clear();
		String[] cl = { "id" };
		listPhones = db.getHeaderPhones(DbConstant.TABLE_HEADER_PHONE_SMS, null,
				null, null, null, null);
		for (HeaderPhone headerPhone : listPhones) {
			Log.i(TAG, headerPhone.toString());
		}
	}

	public void initUpdateSetting() {
		btnPassOk = (Button) findViewById(R.id.btnOkPass);
		btnFakePass = (Button) findViewById(R.id.btnOkFake);
		edtPass = (EditText) findViewById(R.id.edtPassword);
		edtFake = (EditText) findViewById(R.id.edtFakePass);

		group1 = (RadioGroup) findViewById(R.id.radioGroup1);
		group2 = (RadioGroup) findViewById(R.id.radioGroup2);

	}

	public void getEventSetting() {
		final DatabaseHanler db = new DatabaseHanler(this);

		btnPassOk.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				
				String passCode = edtPass.getText().toString();
				db.updateSettingPassCode(passCode);
			}
		});
		int rowUpdate = 0;
		btnFakePass.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				
				String passCode = edtFake.getText().toString();
				db.updateSettingPassFakeCode(passCode);
				db.updateSettingDefault(17);
			}
		});

		final int mode = 0;
		group1.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			Setting s = new Setting();

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				

				s = db.getSetting(null);
				int mode = 0;
				mode = s.getDefaultModel();
				Log.i(TAG, "mode: " + mode + " ModeSms: " + getModeSms(mode));
				switch (checkedId) {
				case R.id.radioButton1:
					db.updateSettingDefault(getModeCall(16) | getModeSms(mode));

					break;
				case R.id.radioButton3:
					db.updateSettingDefault(getModeCall(32) | getModeSms(mode));
					break;
				case R.id.radioButton2:
					db.updateSettingDefault(getModeCall(48) | getModeSms(mode));
					break;
				case R.id.radioButton4:
					db.updateSettingDefault(getModeCall(64) | getModeSms(mode));
					break;
				case R.id.radioButton5:
					db.updateSettingDefault(getModeCall(80) | getModeSms(mode));
					break;
				case R.id.radioButton6:
					db.updateSettingDefault(getModeCall(96) | getModeSms(mode));
					break;
				default:
					break;
				}

				s = db.getSetting(null);
				Log.i(TAG, "Setting: " + s.toString());
				Log.i(TAG,
						"binary: "
								+ Integer.toBinaryString(s.getDefaultModel()));
			}
		});
		group2.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			Setting s = new Setting();

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				
				s = db.getSetting(null);
				int mode = 0;
				mode = s.getDefaultModel();
				Log.i(TAG, "mode: " + mode + " ModeSms: " + getModeSms(mode));
				switch (checkedId) {
				case R.id.radio0:
					db.updateSettingDefault(getModeCall(mode) | getModeSms(1));
					break;
				case R.id.radio1:
					db.updateSettingDefault(getModeCall(mode) | getModeSms(2));
					break;
				case R.id.radio2:
					db.updateSettingDefault(getModeCall(mode) | getModeSms(4));
					break;
				default:
					break;
				}
				s = db.getSetting(null);
				Log.i(TAG, "Setting: " + s.toString());
				Log.i(TAG,
						"binary: "
								+ Integer.toBinaryString(s.getDefaultModel()));
			}
		});
	}

	public int getModeSms(int mode) {
		return mode & 15;
	}

	public int getModeCall(int mode) {
		return mode & 240;
	}

	public void getFormWidget() {
		DatabaseHanler db = new DatabaseHanler(this);
		BlackList = db.getBlackListItem(null, null, null, null, null);
		adaterBlackList = new AdapterBlackList(this, BlackList);
		lvBlackListItem.setAdapter(adaterBlackList);
		lvBlackListItem = (ListView) findViewById(R.id.lvBlackList);
	}

	public void getEvent() {
		lvBlackListItem.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				
				showInfoBlackListItem();
			}
		});
		lvBlackListItem
				.setOnItemLongClickListener(new OnItemLongClickListener() {

					@Override
					public boolean onItemLongClick(AdapterView<?> parent,
							View view, int position, long id) {
						return false;
					}
				});
	}

	public void showInfoBlackListItem() {
		Toast.makeText(this, "on item click!", Toast.LENGTH_SHORT).show();
	}

	public void updateSetting() {

	}

	
}
